package TUILayer;

import java.util.Scanner;
import ControlLayer.*;
/**
 * This class is part of an address book system. It's the AddressBook part of the system UI. This menu is associated with the MainMenuUI class. 
 *
 * The purpose of this class is to communicate with the user and send the requests to the controllayer. 
 * 
 * @author Anders A.P, Anna K., Danny M.P., Jakob K., Mie P.  
 * @version 1.0
 */
public class AddressBookUI
{   

    private AddressCtr addrCtr;
    
    /**
     * Constructs an instance of AddressBookUI, taking no parameters.
     */
    public AddressBookUI()
    {
        addrCtr = new AddressCtr();
    }

    /**
     * Initiates the AddressBookUI class.
     */   
    public void start()
    {       
        addressMenu();
        addrCtr = new AddressCtr();
    }

    /**
     * Starts the UI system in the addressbook menu, allowing the user to move through the parts of the menu.
     */     
    private void addressMenu()
    {
       boolean exit = false;
       while(!exit) {
           int choice = writeAddressBookMenu();
           switch (choice) {
               case 1:
                   startFindPerson();
                   break;
               case 2:
                   startCreatePerson();
                   break;
               case 3:
                   startDeletePerson();
                   break;
               case 4:
                   startUpdatePerson();
                   break;
               case 5:
                   startListAllPersons();
                    break;
               default:
                   exit = true;
                   break;
               
           }
       }
    }

    /**
     * Initiates the text in the AddressBookUI, scanning the user inputs and returns the choice to the caller.
     *
     * @return  choice  The users choice of menu.
     */
    private int writeAddressBookMenu()
    {     
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            System.out.print("\n  Make your choice: ");
            
            int choice = inputChoice();
            return choice;
    }

    /**
     * Method for reading the Person id.
     *
     * @return  id  The Person ID.
     */
    private String inputPersonsID(String message)
    {   
        boolean ok = false;
        String id = "";
        while(!ok) {
            Scanner keyboard = new Scanner(System.in);
            System.out.println(message);
            try {
                id = keyboard.nextLine();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }
        return id;
    }
 
    /**
     * Method for reading the users choice of menu.
     *
     * @return  no  The users choice of menu.
     */   
    private int inputChoice()
    {
        boolean valid = false;
        int no = 0;
       // System.out.println("Type the choice: ");
        while(!valid) {
            Scanner keyboard = new Scanner(System.in);
            try {
                no = keyboard.nextInt();
                valid = true;
            }
            catch(Exception e) {
                System.out.println("Input must only be numbers!");
            }
       }
       return no;
    }

    /**
     * Method for reading the Person name.
     *
     * @return  name    The Person name.
     */    
    private String inputName(String message)
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(message);
         String name = keyboard.nextLine();
         return name;
    }

    /**
     * Method for reading the Person postalCode.
     *
     * @return  postalCode  The Person postal code.
     */   
    private String inputPostalCode(String message)
    {
         Scanner keyboard = new Scanner(System.in);  
         //System.out.println(" Write Postal Code:  ");
         System.out.println(message);
         String code = keyboard.nextLine();
         return code;
    }

    /**
     * Method for reading the Person city.
     *
     * @return  city    The Person city.
     */
    private String inputCity(String message)
    {
         Scanner keyboard = new Scanner(System.in);  
         //System.out.println(" Write City:  ");
         System.out.println(message);
         String city = keyboard.nextLine();
         return city;
    } 

    /**
     * Method for reading the Person phone.
     *
     * @return  phone   The Person phone number.
     */
    private String inputPhone(String message)
    {
         Scanner keyboard = new Scanner(System.in);  
         //System.out.println(" Write Phone Number:  ");
         System.out.println(message);
         String phone = keyboard.nextLine();
         return phone;
    } 
     
    /**
     * Method for reading the Person address.
     *
     * @return  address The Person address.
     */
    private String  inputAddress(String message)
    {
        Scanner keyboard = new Scanner(System.in);
        //System.out.println(" Write Address: ");
        System.out.println(message);
        String address = keyboard.nextLine();
        return address;
    }

    /**
     * Method for showing the user's ID.
     *
     * @param   id  The users ID.
     */    
    private void showId(String id)
    {
        System.out.println("ID of the person: " + id);
    }

    /**
     * Method for showing the user's name.
     *
     * @param   name    The users name.
     */ 
    private void showName(String name)
    { 
        System.out.println("The name of the persone: " + name);
    }

    /**
     * Method for showing the user's address.
     *
     * @param   address The users address.
     */ 
    private void showAddress(String address)
    { 
        System.out.println("The address of the person: " + address);
    }

    /**
     * Finds and prints out the Person object to the terminal
     */ 
    private void startFindPerson()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" (1) Find person by ID.");
        System.out.println(" (2) Find person by name.");
        int choice = inputChoice();
        switch (choice) {
            case 1:
                try {
                    //System.out.println("Enter ID of person to be found: ");
                    //String id = keyboard.next().trim();
                    String id = inputPersonsID("Enter ID of person to be found: ");
                    System.out.println(addrCtr.findPersonID(id));
                }
                catch (Exception e) {
                    System.out.println(e);
                }
                break;
            case 2:
                try {
                    //System.out.println("Enter name of person to be found: ");
                    //String name = keyboard.next().trim(); 
                    //System.out.println("Token was: " + name);
                    /*DEMO keyboard.next() Finds and returns the next complete token before the delimiter which is 
                    a white space. Names are divided by a white space, only the first word will be returned. The controller
                    checks both first and family names and will return an exception if only the first name is returned from 
                    the UI. */
                    String name = inputName("Enter name of person to be found: ");
                    System.out.println(addrCtr.findPersonName(name));
                }
                catch (Exception e) {
                    System.out.println(e);    
                }     
                break;
             default:
                break;
        }
        waitInput();
    }

    /**
     * Creates a new Person object and adds it to the container.
     */ 
    private void startCreatePerson() 
    {
        boolean ok = false;
        while (!ok) {    
            try {
                addrCtr.createPerson(inputPersonsID("ID (email address): "), inputName("Name: "), inputAddress("Address: "), 
                inputPostalCode("Postal code: "), inputCity("City: " ), inputPhone("Phone number: "));
                ok = true;
            }               
            catch (Exception e) {
                System.out.println("Fields contain bad data, make sure there are no blanks or null values.");
                waitInput();
            }
        }
    }   

    /**
     * Finds and removes a Person object from the collection.
     */ 
    private void startDeletePerson() 
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("  You have chosen to delete a person. Who do you want to delete");
        startFindPerson();
        System.out.println("  Are you sure you want to delete this person?");
        System.out.println(" (1) Yes");
        System.out.println(" (2) No");
        int choice = inputChoice();
        switch (choice) {
            case 1:
                addrCtr.deletePerson(inputPersonsID("ID of person to be deleted: "));
                break;
            case 2:
                System.out.println("No action taken.");
                break;
            default:
                System.out.println("No action taken.");
                break;
        } 
        waitInput();        
    }

    /**
     * Finds and updates the Person object's fields
     */ 
    private void startUpdatePerson() 
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("  You have chosen to update a person. Who do you want to update");
        startFindPerson();
        //System.out.print("  Enter ID of person you want to update: ");
        addrCtr.updatePerson(inputPersonsID("  Enter ID of person you want to update: "),
                             inputName("New name: "),
                             inputAddress("New address: "),
                             inputPostalCode("New postal code: "),
                             inputCity("New city: "),
                             inputPhone("New phone number: "));
    }

    /**
     * Prints a list of all Person objects in the container to the terminal.
     */ 
    private void startListAllPersons() 
    {
        try {
            System.out.println("\n");
            System.out.print(addrCtr.listAllPersons());
        }
        catch(Exception e) {
            System.out.println("List is empty");
        }
        waitInput();
    }

    /**
     * Allows the text of former prints to stay on screen until a new enter key is recorded
     */     
    private void waitInput()
    {
        System.out.println("\n Hit enter to continue");
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
}
